package com.yocard.yxyd.ui.fragment.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yocard.yxyd.ui.activity.base.SimpleActivity;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by codeest on 16/8/11.
 * 无MVP的Fragment基类
 */

public abstract class SimpleFragment extends Fragment {

    protected View mView;
    protected Activity mActivity;
    protected Context mContext;
    private Unbinder mUnBinder;

    @Override
    public void onAttach(Context context) {
        mActivity = (Activity) context;
        mContext = context;
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(getLayoutId(), null);
        return mView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mUnBinder = ButterKnife.bind(this, view);
        initEventAndData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnBinder.unbind();
    }

    public void showLoadingDialog() {
        showLoadingDialog("加载中...");
    }

    public void showLoadingDialog(String msg) {
        if (getActivity() instanceof SimpleActivity) {
            SimpleActivity activity = (SimpleActivity) getActivity();
            activity.showLoadingDialog(msg);
        }
    }

    public void dismisDialog() {
        if (getActivity() instanceof SimpleActivity) {
            SimpleActivity activity = (SimpleActivity) getActivity();
            activity.dismisDialog();
        }
    }

    protected void showEmptyView(View view) {
        view.setVisibility(View.VISIBLE);
    }

    protected void hiddenEmptyView(View view) {
        view.setVisibility(View.GONE);
    }

    protected abstract int getLayoutId();

    protected abstract void initEventAndData();
}
